package org.jfk.tbox.controller;

import org.jfk.core.controller.BaseController;
import org.jfk.sys.user.User;
import org.jfk.tbox.model.TboxDevice;

public class TboxDeviceController extends BaseController {
	
	public void edit() {
		TboxDevice tboxDevice = TboxDevice.dao.findById(getParaToInt());
		if (tboxDevice == null){
			tboxDevice = new TboxDevice();
		}
		setAttr("tboxDevice", tboxDevice);
	}

	public void save() throws Exception {
		
		TboxDevice tboxDevice = getModel(TboxDevice.class);
		if (tboxDevice.get("id") == null){
			User currUser = super.getCurrUser();
			tboxDevice.set("creator", currUser.getInt("id"));			
			tboxDevice.save();
		}else{
			//对于可以人工输入ID的场景需要增加此判断
			Object o = TboxDevice.dao.findById(tboxDevice.get("id"));
			if (o == null){
				tboxDevice.save();
			}else{
				tboxDevice.update();
			}
		}
		super.renderJson();
	}	
	
	public void list(){
		listColumns = new String[][] { 
				{ "id", "编号" }, 
				{ "sn", "TBOX设备号" },
				{ "imsi", "IMSI号码" }, 
				{ "sim", "SIM卡号码" }, 
				{ "vin", "车辆VIN码" },
				{ "vUser", "车主姓名" },
				{ "vPlate", "车牌号码" },
				{ "vEcall", "紧急联系电话" },
				{ "activeStatusName", "激活状态" }
				};

		
		
		super.setAttrs();
		
		setAttr("page",TboxDevice.dao.paginate(this));
		
	}
	
	public void del() {
		String id = getPara("id");
		TboxDevice.dao.deleteById(id);
		super.renderText("test del");
	}

	
	public void delAll() {
		Integer[] checkedId = super.getParaValuesToInt("id[]");

		for (int i = 0; checkedId != null && i < checkedId.length; i++) {
			TboxDevice.dao.deleteById(checkedId[i]);
		}
		super.renderText("test delAll");
	}
	

}
